package cn.iocoder.an.module.mold.service.upkeepcat2item;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.an.module.mold.controller.admin.upkeepcat2item.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.upkeepcat2item.UpkeepCat2itemDO;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.util.object.BeanUtils;

import cn.iocoder.an.module.mold.dal.mysql.upkeepcat2item.UpkeepCat2itemMapper;

import static cn.iocoder.an.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.an.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.an.module.mold.enums.ErrorCodeConstants.*;

/**
 * 模具保养分类项目对照 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class UpkeepCat2itemServiceImpl implements UpkeepCat2itemService {

    @Resource
    private UpkeepCat2itemMapper upkeepCat2itemMapper;

    @Override
    public Long createUpkeepCat2item(UpkeepCat2itemSaveReqVO createReqVO) {
        // 插入
        UpkeepCat2itemDO upkeepCat2item = BeanUtils.toBean(createReqVO, UpkeepCat2itemDO.class);
        upkeepCat2itemMapper.insert(upkeepCat2item);
        // 返回
        return upkeepCat2item.getId();
    }

    @Override
    public void updateUpkeepCat2item(UpkeepCat2itemSaveReqVO updateReqVO) {
        // 校验存在
        validateUpkeepCat2itemExists(updateReqVO.getId());
        // 更新
        UpkeepCat2itemDO updateObj = BeanUtils.toBean(updateReqVO, UpkeepCat2itemDO.class);
        upkeepCat2itemMapper.updateById(updateObj);
    }

    @Override
    public void deleteUpkeepCat2item(Long id) {
        // 校验存在
        validateUpkeepCat2itemExists(id);
        // 删除
        upkeepCat2itemMapper.deleteById(id);
    }

    @Override
        public void deleteUpkeepCat2itemListByIds(List<Long> ids) {
        // 删除
        upkeepCat2itemMapper.deleteByIds(ids);
        }


    private void validateUpkeepCat2itemExists(Long id) {
        if (upkeepCat2itemMapper.selectById(id) == null) {
            throw exception(UPKEEP_CAT2ITEM_NOT_EXISTS);
        }
    }

    @Override
    public UpkeepCat2itemDO getUpkeepCat2item(Long id) {
        return upkeepCat2itemMapper.selectById(id);
    }

    @Override
    public PageResult<UpkeepCat2itemDO> getUpkeepCat2itemPage(UpkeepCat2itemPageReqVO pageReqVO) {
        return upkeepCat2itemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<UpkeepCat2itemDO> getUpkeepCat2itemByCatId(Long catId) {
        return upkeepCat2itemMapper.selectList(
                new LambdaQueryWrapper<UpkeepCat2itemDO>().eq(UpkeepCat2itemDO::getCategoryId, catId)
        );
    }


    @Override
    public boolean deleteUpkeepCat2itemByCatId(Long catId) {
        // 使用原生SQL删除category_id为catId的所有记录
        return upkeepCat2itemMapper.deleteByCategoryId(catId) > 0;
    }

    public void updateUpkeepCat2itemByCatId(List<UpkeepCat2itemSaveReqVO> updateReqVO) {
        if (CollUtil.isEmpty(updateReqVO)) {
            return;
        }
        // 获取categoryId（假设所有记录的categoryId相同）
        Long categoryId = updateReqVO.get(0).getCategoryId();
        // 先删除该categoryId下的所有记录
        upkeepCat2itemMapper.delete(
                new LambdaQueryWrapper<UpkeepCat2itemDO>()
                        .eq(UpkeepCat2itemDO::getCategoryId, categoryId)
        );

        // 批量插入新记录
        List<UpkeepCat2itemDO> toInsertList = convertList(updateReqVO,
                vo -> BeanUtils.toBean(vo, UpkeepCat2itemDO.class));

        toInsertList.forEach(upkeepCat2itemMapper::insert);
    }

}